/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#pragma once

#include <stdint.h>
#include <vector>

#define HEART_BEAT_SEQNO -1

namespace Hdfs {
    namespace Internal {
        class ConstPacketBuffer {
        public:
            ConstPacketBuffer(const char *buf, int size) : buffer(buf), size(size) {
            }

            const char *getBuffer() const {
                return buffer;
            }

            const int getSize() const {
                return size;
            }

        private:
            const char *buffer;
            const int size;
        };

        /**
         * buffer is pointed into like follows:
         *  (C is checksum data, D is payload data)
         *
         * [HHHHHCCCCC________________DDDDDDDDDDDDDDDD___]
         *       ^    ^               ^               ^
         *       |    checksumPos     dataStart       dataPos
         *   checksumStart
         */
        class Packet {
        public:
            /**
             * create a heart beat packet
             */
            Packet();

            /**
             * create a new packet
             */
            Packet(int pktSize, int chunksPerPkt, int64_t offsetInBlock, int64_t seqno, int checksumSize);

            void reset(int pktSize, int chunksPerPkt, int64_t offsetInBlock, int64_t seqno, int checksumSize);

            void addChecksum(uint32_t checksum);

            void addData(const char *buf, int size);

            void setSyncFlag(bool sync);

            void increaseNumChunks();

            bool isFull();

            bool isHeartbeat();

            void setLastPacketInBlock(bool lastPacket);

            int getDataSize();

            const ConstPacketBuffer getBuffer();

            int64_t getLastByteOffsetBlock();

            int64_t getSeqno() const {
                return seqno;
            }

            bool isLastPacketInBlock() const {
                return lastPacketInBlock;
            }

            int64_t getOffsetInBlock() const {
                return offsetInBlock;
            }

        private:
            bool lastPacketInBlock; // is this the last packet in block
            bool syncBlock; // sync block to disk?
            int checksumPos;
            int checksumSize;
            int checksumStart;
            int dataPos;
            int dataStart;
            int headerStart;
            int maxChunks; // max chunks in packet
            int numChunks; // number of chunks currently in packet
            int64_t offsetInBlock; // offset in block
            int64_t seqno; // sequence number of packet in block
            std::vector<char> buffer;
        };
    }
}
